package com.jiang.controller;

import com.jiang.anno.Log;
import com.jiang.pojo.Emp;
import com.jiang.pojo.PageBean;
import com.jiang.pojo.Result;
import com.jiang.service.EmpService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired  // 面向接口编程
    private EmpService empService;

    // 员工列表查询
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("分页查询 ：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);

        PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);

        return Result.success(pageBean);
    }


    // 删除员工信息
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除员工：ids:{}",ids);

        empService.delete(ids);
        return Result.success();
    }

    // 新增员工信息
    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工，emp：{}",emp);
        empService.save(emp);
        return Result.success();
    }


    /**
     * 根据ID查询员工信息
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据ID查询员工{} 的信息",id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更新员工：{}的信息",emp.getId());
        empService.update(emp);
        return Result.success();
    }

}
